package cn.zktr.Personnel.model.service.zhouyongcong.end;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.pojos.PerPmsFun;
import cn.zktr.Personnel.model.pojos.PerPmsFuns;
import cn.zktr.Personnel.model.pojos.PerPmsPes;
import cn.zktr.Personnel.model.pojos.PerPmsYG;
import cn.zktr.Personnel.model.pojos.PerPmsYQ;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;
import cn.zktr.Personnel.model.service.zhouyongcong.saveService;

@Service
@Transactional(rollbackFor=Exception.class)
public class authorityService {
	@Autowired
	private findService fs;
	@Autowired
	private saveService ss;
	
	public List<PerPmsPes> tail_1(Integer empid) {
		List<PerPmsYQ> list = fs.findEmpid(empid).getPerPmsYQs();
		List<PerPmsPes> list_1 = new ArrayList<>();
		for(PerPmsYQ ppyq : list) {
			PerPmsPes ppp = ppyq.getPerPmsPe();
			PerPmsPes ppp1 = new PerPmsPes();
			ppp1.setPesgad(ppp.getPesgad());
			ppp1.setPesid(ppp.getPesid());
			ppp1.setPesname(ppp.getPesname());
			ppp1.setPesurl(ppp.getPesurl());
			list_1.add(ppp1);
		}
		return list_1;
	}
	public List<PerPmsFun> tail_2(int empid,String tatext){
		List<PerPmsYG> list = fs.findEmpid(empid).getPerPmsYGs();
		List<PerPmsFun> list_1 = new ArrayList<>();
		for(PerPmsYG ppyg : list) {
			if(ppyg.getPerPmsFun().getPerPmsPe().getPesname().equals(tatext)) {
				PerPmsFun ppf = ppyg.getPerPmsFun();
				PerPmsFun ppf1 = new PerPmsFun();
				ppf1.setFungad(ppf.getFungad());
				ppf1.setFunid(ppf.getFunid());
				ppf1.setFunname(ppf.getFunname());
				ppf1.setFunurl(ppf.getFunurl());
				List<PerPmsFuns> list_2 = new ArrayList<>();
				for(PerPmsFuns ppfs : ppf.getPerPmsFuns()) {
					if(ppfs.getFunsgad().equals("3")) {
						PerPmsFuns ppfs2 = new PerPmsFuns();
						ppfs2.setFunsgad(ppfs.getFunsgad());
						ppfs2.setFunsid(ppfs.getFunsid());
						ppfs2.setFunsname(ppfs.getFunsname());
						ppfs2.setFunsurl(ppfs.getFunsurl());
						list_2.add(ppfs2);
					}
				}
				ppf1.setPerPmsFuns(list_2);
				list_1.add(ppf1);
			}
		}
		return list_1;
	}
	public void aut_emp(Integer empid,Integer id,String sf,String aut) {
		PerPmsEmp ppe = fs.findEmpid(empid);
		if(aut.equals("pes")) {
			List<PerPmsYQ> l_ppyq = ppe.getPerPmsYQs();
			for(PerPmsYQ ppyq : l_ppyq) {
				if(ppyq.getPerPmsPe().getPesid()==id&&!ppyq.getYqwht().equals(sf)) {
					ss.save_YQ(ppyq.getYqid(),sf);
				}
			}
		}
	}
	public void aut_emf(int empid,int[] funid,String[] fun) {
		PerPmsEmp ppe = fs.findEmpid(empid);
		List<PerPmsYG> list_ppyg = ppe.getPerPmsYGs();
		if(funid!=null) {
			for(int i=0;funid.length>i;i++) {
				ss.save_YG(list_ppyg.get(funid[i]-1).getYgid(), fun[i]);
			}
		}
	}
}
